#!/bin/sh
export NOTIFY_URL="http://120.78.19.55:9201/JenkinsCallback"
export FTP_SERVER="http://cdn.hpplay.com.cn/jenkins_result"
export FTP_DIR="lebotest@10.24.246.224:/lebores/jenkins_result"
echo this is android receiver apk build ...

function Error_Msg()
{
	error_msg=$1
	echo error_message=$error_msg
	if [ "$JOB_NAME" != "Phoenix-App" ];then
		curl -H "Content-Type:application/json"  -X POST --data  '{"errorinfo": [{"error_message":"'"$error_msg"'","jid":"'$rp_jid'"}]}'  "$notify_url_test"
	else
		curl -H "Content-Type:application/json"  -X POST --data  '{"errorinfo": [{"error_message":"'"$error_msg"'","jid":"'$rp_jid'"}]}'  "$NOTIFY_URL"
	fi
	exit 1
}


function phoenix_app_special_channels()
{
    tool_path="/root/helen/scripts"
    echo app_name=$1 and channel=$2
    if [ "$2" != "pptv" -a "$2" != "xiaomi" ];then 
        echo not pptv and xiaomi channel
        #return 
    fi
    if [ -d sign_apk_tool ];then
        echo delete sign_apk_tool
        rm sign_apk_tool -rf 
    fi 
    mkdir sign_apk_tool
    mv target/$1 sign_apk_tool/ 
    cd sign_apk_tool/ 
    old_apk=$1
    new_channel=$2
    old_apk_name=`echo $old_apk |awk -F ".apk" '{print $1}'`
    echo old_apk_name=$old_apk_name 

    echo ================================apktool decompile===========================
    java -jar $tool_path/apktool.jar d $old_apk
    echo delete $old_apk ...
    rm $old_apk 
    echo ==============================delete META-INF ===================
    #pptv有大小限制，删除homepage
    if [ "$2" == "pptv" ];then
        echo pptv channel delete assets/hpplay/homepage
        rm -f $old_apk_name/assets/hpplay/homepage
        echo check delete result
        ls $old_apk_name/assets/hpplay/
    fi

    #alibaba和康佳的apk名字和启动页上要加上CIBN字样
    if [ "$2" == "alibaba" -o "$2" == "konka" ];then
        echo $2 channel need to change app name and add "CIBN"
        sed -i s/app_name\"\>/app_name\"\>CIBN/g   $old_apk_name/res/values/strings.xml
        sed -i s/app_name\"\>/app_name\"\>CIBN/g   $old_apk_name/res/values-zh-rCN/strings.xml
        sed -i s/app_name\"\>/app_name\"\>CIBN/g   $old_apk_name/res/values-zh-rHK/strings.xml
        sed -i s/app_name\"\>/app_name\"\>CIBN/g   $old_apk_name/res/values-zh-rTW/strings.xml
        echo change result as follow:
        grep -rn app_name $old_apk_name/res/* |grep -v public.xml
	echo change lunch page to CIBN lunch page_244k
	cp  $tool_path/ali/ali_lunch.png $old_apk_name/res/mipmap-xhdpi-v4/lunch_bg.png
        du -sh $old_apk_name/res/mipmap-xhdpi-v4/lunch_bg.png        
    fi

     #大麦(1)修改apk名字,(2)替换bg_main_video.png(3)替换优化页(4)替换icon(5)替换启动页(6)替换devicename.txt文件
    if [ "$2" == "damai_inner" -o "$2" == "damai_oversea" ];then
        echo $2 channel need to change app name and add damai
        sed -i s/app_name\"\>/app_name\"\>Damai/g   $old_apk_name/res/values/strings.xml
        sed -i s/app_name\"\>/app_name\"\>"大麦"/g   $old_apk_name/res/values-zh-rCN/strings.xml
        sed -i s/app_name\"\>/app_name\"\>"大麥"/g   $old_apk_name/res/values-zh-rHK/strings.xml
        sed -i s/app_name\"\>/app_name\"\>"大麥"/g   $old_apk_name/res/values-zh-rTW/strings.xml
        echo change result as follow:
        grep -rn app_name $old_apk_name/res/* |grep -v public.xml

        echo replace bg_main_video.png_12K and result as follow:
        cp $tool_path/damai/bg_main_video.png $old_apk_name/res/mipmap-xxhdpi-v4/bg_main_video.png
        du -sh  $old_apk_name/res/mipmap-xxhdpi-v4/bg_main_video.png

        echo replace cast_optimize.png_608k and result as follow:
        cp $tool_path/damai/cast_optimize.png $old_apk_name/res/mipmap-xhdpi-v4/cast_optimize.png
        du -sh $old_apk_name/res/mipmap-xhdpi-v4/cast_optimize.png

        echo replace lunch page_104k and result as follow:
        cp $tool_path/damai/damai_lunch_pg.png   $old_apk_name/res/mipmap-xhdpi-v4/lunch_bg.png
        du -sh $old_apk_name/res/mipmap-xhdpi-v4/lunch_bg.png

        echo replace devicename.txt and result as follow:
        cp $tool_path/damai/devicenames.txt   $old_apk_name/res/raw/devicenames.txt
        cat $old_apk_name/res/raw/devicenames.txt
        echo

	echo damai channel delete assets/hpplay/homepage
        rm -f $old_apk_name/assets/hpplay/homepage
        echo check delete result
        ls $old_apk_name/assets/hpplay/ 
    fi

    #alibaba使用不同的世界杯图标
    if [ "$2" == "alibaba" ];then
        echo alibaba change to world cup icon_68k
        rm $old_apk_name/res/mipmap-xhdpi-v4/lebo_game_icon.png
        cp  $tool_path/ali/ali_world_cup.png  $old_apk_name/res/mipmap-xhdpi-v4/lebo_game_icon.png
        du -sh $old_apk_name/res/mipmap-xhdpi-v4/lebo_game_icon.png
    fi

    #替换图标为大麦的图标
    if [ "$2" == "damai_inner" -o  "$2" == "damai_oversea" ];then
        echo $2 replace icon_20K and result as follow:
        rm $old_apk_name/res/mipmap-xhdpi-v4/lebo_game_icon.png
        cp $tool_path/damai/damai_icon.png  $old_apk_name/res/mipmap-xhdpi-v4/lebo_game_icon.png
        du -sh $old_apk_name/res/mipmap-xhdpi-v4/lebo_game_icon.png
    fi

    #替换默认图标为世界杯图标
    if [ "$2" != "alibaba" -a  "$2" != "damai_inner" -a "$2" != "damai_oversea" ];then
        echo $2 channel change to world cup icon
        rm $old_apk_name/res/mipmap-xhdpi-v4/lebo_game_icon.png
        cp  $tool_path/world_cup.png $old_apk_name/res/mipmap-xhdpi-v4/lebo_game_icon.png
    fi

    rm -rf $old_apk_name/original/META-INF 
    echo ================================apktool compile============================
    java -jar $tool_path/apktool.jar b $old_apk_name
    mv $old_apk_name/dist/$old_apk ./ 

#echo setting alias .....
#keytool -list -v -keystore mirrorcast_aw.key -alias mirrorcast_aw -storepass mirrorcastisgood
    echo ===================================sign====================================
    jarsigner -verbose -digestalg SHA1 -sigalg SHA1withRSA -keystore $tool_path/mirrorcast_aw.key -signedjar "$old_apk_name".apk $old_apk mirrorcast_aw -storepass mirrorcastisgood

    echo ==================================checking sign=============================
#jarsigner -certs -verbose -verify "$old_apk_name"_signed.apk
    jarsigner -certs -verify "$old_apk_name".apk
    mv "$old_apk_name".apk  ../target/"$old_apk_name".apk
    cd ../
    du -sh target/*
    echo cur_path=`pwd`
}

sed -i 's/,/\n/g' user_config
source /etc/profile
echo jenkins var as follow:
echo build_num=$BUILD_NUMBER
echo build_user_id=$BUILD_USER_ID
echo build_user=$BUILD_USER
echo job_name=$JOB_NAME
if [ "$JOB_NAME" != "Phoenix-App" ];then
	echo ====================Test Project===========
	ftp_server_test="http://cdn.hpplay.com.cn/jenkins_result_test"
	ftp_dir_test="lebotest@10.24.246.224:/lebores/jenkins_result_test"
	notify_url_test="http://120.79.212.106:9201/JenkinsCallback"
fi
 
rp_build_appid=`grep  build_appid user_config |awk -F '=' '{print $2}'`
rp_build_secret=`grep  build_secret user_config |awk -F '=' '{print $2}'`
rp_build_path=`grep  build_aliyun_path user_config |awk -F '=' '{print $2}'`
rp_jid=`grep jid user_config |awk -F '=' '{print $2}'`
echo user config as follow:
cat user_config
echo current path is `pwd`
echo get string build_appid=$rp_build_appid,build_secret=$rp_build_secret,build_path=$rp_build_path,jid=$rp_jid

if [ "x$rp_build_appid" == "x" ];then
	error_message="appid is null"
	Error_Msg "$error_message"
fi 

if [ "x$rp_build_secret" == "x" ];then
	error_message="secret is null"
	Error_Msg "$error_message"
fi

if [ "x$rp_build_path" == "x" ];then
	error_message="aliyun path is null"
	Error_Msg "$error_message"
fi

rec_flag=`echo $rp_build_path |grep "phoenix_apk"`
send_flag=`echo $rp_build_path |grep "phoenix_send_apk"`
echo rec_flag=$rec_flag and send_flag=$send_flag
if [ "x$rec_flag" == "x" -a "x$send_flag" == "x" ];then
	error_message="aliyun path is not phoenix rec app nor phoenix send app"
	Error_Msg "$error_message"
fi
 
apk_name=phoenix_apk
echo apk_name=$apk_name
echo download release app:
curl -o phoenix_apk.zip $rp_build_path
if [ $? -ne 0 ];then
	error_message="download aliyun path file failed,maybe path is not right"
	Error_Msg "$error_message"
fi 
if [ ! -f phoenix_apk.zip ]; then
	error_message="download aliyun path file failed,maybe path is not right"
	Error_Msg "$error_message"
fi
unzip "$apk_name".zip
if [ $? -ne 0 ];then
	error_message="download aliyun path file failed,maybe path is not right"
	Error_Msg "$error_message"
fi
 
new_apk_name=`ls |grep ".apk$" |awk -F ".apk" '{print $1}' `
echo unzip app name=$new_apk_name
mkdir target
mv "$new_apk_name".apk target/
if [ "x$rec_flag" != "x" ];then
	cp /root/helen/scripts/rec_app_info.conf ./
	appid_flag=`grep "^app_id=$rp_build_appid$" rec_app_info.conf `
	if [ "x$appid_flag" == "x" ];then
		echo appid is not in rec_app_info.conf, so use channel allcast
		rp_build_channel="allcast"
	else
		tmp_line2=`grep -rn "^app_id=$rp_build_appid$" rec_app_info.conf |awk -F ":" '{print $1}'`
		tmp_line3=`expr $tmp_line2 - 1`
		rp_build_channel=`sed -n "$tmp_line3"p rec_app_info.conf |awk -F ":" '{print $1}'`
	fi
	echo channle=$rp_build_channel,secret=$rp_build_secret
	phoenix_app_special_channels "$new_apk_name".apk $rp_build_channel
	recapp_path=phoenix_apk
fi

if [ "x$send_flag" != "x" ];then
	rp_build_channel="lebotest"
	recapp_path=phoenix_send_apk
fi
mv target/"$new_apk_name".apk ./

mkdir apk_output
mkdir META-INF
touch META-INF/appid_$rp_build_appid
touch META-INF/secret_$rp_build_secret
touch META-INF/channel_$rp_build_channel
echo new MRTA-INF file as follow:`ls META-INF`
echo "file type as follow (file command returen empty means empty file ):"
file META-INF/appid_$rp_build_appid 
file META-INF/secret_$rp_build_secret
file META-INF/channel_$rp_build_channel
echo zip add null file to "$new_apk_name".apk
zip -m "$new_apk_name".apk ./META-INF/*

time_now=`date "+%Y%m%d%H%M"`
new_apk_name2=`ls |grep ".apk$" |awk -F ".apk" '{print $1}' |sed s/happytest/$rp_build_appid/g `
app_ver=`ls |grep ".apk$" |awk -F "-v" '{print $2}' |awk -F ".apk" '{print $1}' |awk -F "_" '{print $1}' | sed 's/\.//g' `
echo "mv "$new_apk_name".apk apk_output/"$new_apk_name2".apk"
mv "$new_apk_name".apk apk_output/"$new_apk_name2".apk
cd apk_output
md5sum "$new_apk_name2".apk > md5sum.txt
upload_app="$time_now"_"$recapp_path"_"$rp_build_appid"_"$rp_build_channel"_"$app_ver"
zip -r "$upload_app".zip "$new_apk_name2".apk md5sum.txt
zip_md5sum=`md5sum "$upload_app".zip |awk -F " " '{print $1}'`
echo zip empty file to apk as follow:

mkdir unzip_apk
unzip  -q "$new_apk_name2".apk -d unzip_apk/
ls unzip_apk/META-INF/
if [ -f unzip_apk/META-INF/appid_$rp_build_appid ];then
	echo target file appid_$rp_build_appid in unzip META-INF ,success!
else
	error_message="target file appid_$rp_build_appid not in unzip META-INF ,fail"
	Error_Msg "$error_message"
fi
tapp_name="$time_now"_"$new_apk_name2".apk
mv "$new_apk_name2".apk $tapp_name
app_md5sum=`md5sum $tapp_name |awk -F " " '{print $1}'`
echo will upload file "$upload_app".zip and $tapp_name to ftp server dir $recapp_path
echo upload file info as follow:
du -sh "$upload_app".zip
du -sh $tapp_name
if [ "$JOB_NAME" == "Phoenix-App" ];then
	sshpass -p "lebo123"  scp  "$upload_app".zip $FTP_DIR/$recapp_path/
	recapp_download="$FTP_SERVER/$recapp_path/"$upload_app".zip"
	sshpass -p "lebo123"  scp  $tapp_name  $FTP_DIR/$recapp_path/
	apk_download="$FTP_SERVER/$recapp_path/$tapp_name"
	echo download_path=$recapp_download
	echo download_apk_path=$apk_download
	echo 
	curl -H "Content-Type:application/json"  -X POST --data '{"dlinfo": [{"result_path": "'$recapp_download'","jid": "'$rp_jid'","aversion": "'$app_ver'","bversion": "null","furl": "'$recapp_download'","fmd5": "'$zip_md5sum'","apkurl":"'$apk_download'","apkmd5":"'$app_md5sum'","purl": "null","pmd5": "null" }]}' $NOTIFY_URL 
else
	sshpass -p "lebo123"  scp  "$upload_app".zip $ftp_dir_test/$recapp_path/
	recapp_download="$ftp_server_test/$recapp_path/"$upload_app".zip"
	sshpass -p "lebo123"  scp  $tapp_name $ftp_dir_test/$recapp_path/
	apk_download="$ftp_server_test/$recapp_path/$tapp_name"
	echo download_path=$recapp_download
	echo download_apk_path=$apk_download
	echo 
	curl -H "Content-Type:application/json"  -X POST --data '{"dlinfo": [{"result_path": "'$recapp_download'","jid": "'$rp_jid'","aversion": "'$app_ver'","bversion": "null","furl": "'$recapp_download'","fmd5": "'$zip_md5sum'","apkurl":"'$apk_download'","apkmd5":"'$app_md5sum'","purl": "null","pmd5": "null" }]}' $notify_url_test  
fi

#curl -H "Content-Type:application/json"  -X POST --data '{"dlinfo": [{"result_path": "'"$recapp_download"'","jid": "'$rp_jid'"}]}' $NOTIFY_URL 
#sshpass -p "lebo123"  scp -v "$upload_app" $FTP_DIR/$recapp_path/
#scp -o GSSAPIAuthentication=no -c arcfour128  -v "$upload_app" root@120.76.155.128:/lebores/jenkins_result/$recapp_path/
#sshpass -p "lebo123"  rsync -av --progress  "$upload_app" $FTP_DIR/$recapp_path/
#cp -f /root/helen/scripts/expect_scp ./
#ftp_pwd="/lebores/jenkins_result"
#./expect_scp 120.76.155.128  "lebotest" "lebo123"  "$upload_app" $ftp_pwd/$recapp_path/
#start_time=`date +%s`
#end_time=`date +%s`
#cost=$(( $end_time - $start_time ))
#echo "upload  Done. Total costs $cost seconds."
